#include "GlobalHead.h"
#include "datatree.h"
#include "Tree.h"
#include "Gui_Document.h"
#include "Gui_Application.h"
#include "BitmapFactory.h"
#include "Command.h"
#include <QDir>   
#include <QFile>  
#include "CustomPlayer.h"

using namespace Gui;

QPixmap*  DataTreeWidget::documentPixmap = 0;

DataTreeWidget::DataTreeWidget(QWidget* parent)
  : QTreeWidget(parent)
{
 	this->setDragEnabled(true);
 	this->setAcceptDrops(false);
 	this->setDropIndicatorShown(false);
 	this->setRootIsDecorated(false);

	Gui_Application::instance->signalNewDocument.connect(boost::bind(&DataTreeWidget::slotNewDocument, this, _1));

	QStringList labels;
	labels << tr("Labels & Attributes");
	this->setHeaderLabels(labels);

	// Add the first main label
	this->rootItem = new QTreeWidgetItem(this);
	this->rootItem->setText(0, tr("Application10102"));
	this->rootItem->setFlags(Qt::ItemIsEnabled);
	this->expandItem(this->rootItem);


	this->setSelectionMode(QAbstractItemView::ExtendedSelection);
 #if QT_VERSION >= 0x040200
 	// causes unexpected drop events (possibly only with Qt4.1.x)
 	this->setMouseTracking(true); // needed for itemEntered() to work
 #endif

	documentPixmap = new QPixmap(BitmapFactory().pixmap("Document"));

//  	QTreeWidgetItem* item = new QTreeWidgetItem(this->rootItem);
//  
//  	this->expandItem(item);
//  	item->setIcon(0, *documentPixmap);
//  	item->setText(0, QString::fromUtf8("fsafesafesfe"));
}

void DataTreeWidget::slotNewDocument(const Gui_Document& pDoc)
{
	// QTreeWidgetItem* item = new QTreeWidgetItem(rootItem);
   // DataItem* item = new DataItem(&pDoc,this->rootItem);

	//item->updataOutTreeData("");
// 	this->expandItem(item);
// 	item->setIcon(0, *documentPixmap);
// 	item->setText(0, QString::fromUtf8("uuuuuuu"));
// 
// 	QTreeWidgetItem* childitem = new QTreeWidgetItem(item);
// 
// 	//this->expandItem(childitem);
// 	childitem->setIcon(0, *documentPixmap);
// 	childitem->setText(0, QString::fromUtf8("hahahaa"));
// 
// 	QTreeWidgetItem* cccitem = new QTreeWidgetItem(childitem);
// 
// 	cccitem->setIcon(0, *documentPixmap);
// 	cccitem->setText(0, QString::fromUtf8("bbvvff"));
	QString sg = QCoreApplication::applicationDirPath();
	QString da = QString::fromAscii("/data");
	sg.append(da);
	searchDataFile(sg/*QString::fromAscii("E:\\work\\sh_vr_city\\DigitalCity\\dcSln\\Debug\\data")*/,rootItem);
  
// 	item->addChild(childitem);
// 	item->addChild(cccitem);


}

int DataTreeWidget::searchDataFile(const QString& sComFilePath,QTreeWidgetItem* wItem)
{ 
	QDir dir(sComFilePath);  
	if (!dir.exists()) {  
		return -1;  
	}  
	dir.setFilter(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot);  
	dir.setSorting(QDir::DirsFirst);  
	QFileInfoList list = dir.entryInfoList();  
	if(list.size()< 1 ) {  
		return -1;  
	}  
	int i=0;  
   QTreeWidgetItem* childitem = NULL;

	do{  
		QFileInfo fileInfo = list.at(i);  
		QString tt = fileInfo.fileName();  
		bool bisDir = fileInfo.isDir(); 
		if(bisDir) {  
			QString qs = fileInfo.fileName();
			QTreeWidgetItem* childitem = new QTreeWidgetItem(wItem);
			childitem->setIcon(0, *documentPixmap);
			childitem->setText(0,qs);
			searchDataFile(fileInfo.filePath(),childitem); 
		}  
		else{  
			bool isDll = fileInfo.fileName().endsWith(QString::fromAscii(".nmo"));  
			if (isDll == true) {  
              QString qs = fileInfo.fileName();
             QTreeWidgetItem* fItem = new QTreeWidgetItem(wItem);
			  fItem->setIcon(0, *documentPixmap);
			  fItem->setText(0,qs);
			  fItem->setData(0,Qt::UserRole,fileInfo.filePath());
			}  
		}
		i++;  
	} while(i < list.size());  
}

void DataTreeWidget::updataOutTreeData(QString path)
{

}
void DataTreeWidget::traverseFile(QString path)
{

}
void DataTreeWidget::insetDataValue()
{

}

DataTreeWidget::~DataTreeWidget()
{
   delete documentPixmap;
   documentPixmap = NULL;
}


void DataTreeWidget::onCreateGroup()
{
}

void DataTreeWidget::onRelabelObject()
{
}

void DataTreeWidget::onActivateDocument(QAction*)
{
}

void DataTreeWidget::onStartEditing()
{
}

void DataTreeWidget::onFinishEditing()
{
}

void DataTreeWidget::onItemSelectionChanged(void)
{
}

void DataTreeWidget::onItemEntered(QTreeWidgetItem * item)
{
	// object item selected
	if ( item && item->type() == TreeWidget::ObjectType ) {
		DocumentObjectItem* obj = static_cast<DocumentObjectItem*>(item);
		obj->displayStatusInfo();
	}
}

void DataTreeWidget::onTestStatus(void)
{
}

void DataTreeWidget::onSelectionChanged(const SelectionChanges& msg)
{

}
void DataTreeWidget::contextMenuEvent (QContextMenuEvent * e)
{

}
void DataTreeWidget::drawRow(QPainter *painter, const QStyleOptionViewItem &options, const QModelIndex &index) const
{
    QTreeWidget::drawRow(painter, options, index);
}
// bool DataTreeWidget::dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data,
// 	Qt::DropAction action)
// {
//    return QTreeWidget::dropMimeData(parent, index, data, action);
// }
// Qt::DropActions DataTreeWidget::supportedDropActions () const
// {
//    return QTreeWidget::supportedDropActions();
// }
 QMimeData * DataTreeWidget::mimeData (const QList<QTreeWidgetItem *> items) const
 {


	 QByteArray itemData;
	 QDataStream dataStream(&itemData, QIODevice::WriteOnly);
	 dataStream << items.count();

	 for (QList<QTreeWidgetItem*>::ConstIterator it = items.begin(); it != items.end(); ++it) 
	 {
		 if((*it)->childCount()>0)
			 return NULL;

		 QVariant value  = (*it)->data(0,Qt::UserRole);
		 QString str = value.toString();
		 dataStream << str;

	 }

	 QMimeData *mimeData = new QMimeData;
	 mimeData->setData(QString::fromAscii("text/x-action-items"), itemData);

	 return mimeData;


// 	 QList<QString> _names;
// 	 QList<QTreeWidgetItem*>::const_iterator it = items.begin(); 
// 	 for (; it != items.end(); ++it)
// 	 {
// 		 _names.push_back((*it)->text(0));
// 	 }
// 
// 
// 
// 
// 	    QMimeData *mimeData = new QMimeData();
// 		  mimeData->setData(QString::fromAscii("fsafe"),_names);
// 		//mimeData->setData("text/x-action-items",_names);
//      return mimeData;
    // return QTreeWidget::mimeData(items);
 }//

//  void DataTreeWidget::startDrag(Qt::DropActions supportedActions)
//  {
//       QList<QTreeWidgetItem*> items = selectedItems();
// 	  for (QList<QTreeWidgetItem*>::const_iterator it = items.begin(); it != items.end();++it)
// 	  {
//        if((*it)->childCount()>0)
// 		   return;
// 	  }
// 	  
//  }
//  void DataTreeWidget::dragMoveEvent(QDragMoveEvent *event)
//  {
//      QTreeWidget::dragMoveEvent(event);
//  	if(!event->isAccepted())
//  		return;
//  }
//  void DataTreeWidget::dropEvent(QDropEvent *event)
//  {
//     
//  }
void DataTreeWidget::mouseDoubleClickEvent(QMouseEvent * event)
{
   QTreeWidgetItem* targetitem = itemAt(event->pos());

   if(!targetitem)
	   return;
   if(targetitem->childCount() == 0)
	   return;

	   if(targetitem->isExpanded())	   
          targetitem->setExpanded(false);
	   else
		   targetitem->setExpanded(true);
}

#include "moc_datatree.cpp"


DataItem::DataItem(const Gui_Document* doc, QTreeWidgetItem * parent)
:pDocument(doc)
{
       setFlags(Qt::ItemIsEnabled/*|Qt::ItemIsEditable*/);
}
DataItem::~DataItem()
{
	
}

const Gui_Document* DataItem::document() const
{
   return pDocument;
}
void DataItem::setObjectHighlighted(const char*, bool)
{

}
void DataItem::setObjectSelected(const char*, bool)
{

}
void DataItem::clearSelection(void)
{

}
void DataItem::updateSelection(void)
{

}
void DataItem::selectItems(void)
{

}
void DataItem::testStatus(void)
{

}
void DataItem::setData(int column, int role, const QVariant & value)
{

}

void DataItem::slotNewObject(const Gui::ViewProviderDocumentObject&)
{
      //ObjectMap[] = 
}

void DataItem::updataOutTreeData(QString path)
{
// 	DataItem* ditem = new DataItem(pDocument,this->rotatetool)
//     ObjectMap[]
}
